package com.zxk.version4.consumer;

import com.zxk.version4.core.RpcRequest;
import com.zxk.version4.core.RpcResponse;

import java.io.*;
import java.net.Socket;

/**
 * @author： 朱小康
 * @create： 2024/5/29 17:04
 */
public class IOClient {

    //负责底层与服务端的通信，发送request 接受response

    public static RpcResponse sendRequest(String host, int port, RpcRequest request){

        try {
            Socket socket = new Socket(host,port);
            InputStream inputStream = socket.getInputStream();
            OutputStream outputStream = socket.getOutputStream();
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
            ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);

            objectOutputStream.writeObject(request);
            objectOutputStream.flush();

            RpcResponse response = (RpcResponse) objectInputStream.readObject();

            return response;

        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }
}
